package services.frame;

import com.ani.mydragon.dto.template.*;
import controllers.routes;
import models.user.*;
import services.AniPublicPropService;
import services.application.*;
import utils.AniGeneralUtils;
import utils.AniMapBuilder;
import utils.MVCUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * User: yeh
 * Date: 3/18/13
 * Time: 5:10 PM
 * For:  wilddragon
 */
public class AniAppFrameService extends AniFrameService {
    public static PageTemplate appDetailPage(){
        List<PageObjContainer> detailObjects = new ArrayList<PageObjContainer>();
        //installed or remove
        //add button
        detailObjects.add(appInstallButton());
        detailObjects.add(appActivateButton());
        detailObjects.add(
                new PageObjContainer(
                        "appName",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "appName",
                                "APP_NAME",
                                "LOADING"
                        ),
                        "appName"
                )
        );
        detailObjects.add(
                new PageObjContainer(
                        "appId",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "appId",
                                "APP_ID",
                                "LOADING"
                        ),
                        "id"
                )
        );
        detailObjects.add(
                new PageObjContainer(
                        "appToken",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "appToken",
                                "APP_TOKEN",
                                "LOADING"
                        ),
                        "token"
                )
        );
        detailObjects.add(
                new PageObjContainer(
                        "classification",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "classification",
                                "APP_CLASSIFICATION",
                                "LOADING"
                        ),
                        "appClassification.classificationKey"
                )
        );
        detailObjects.add(
                new PageObjContainer(
                        "forwardUrl",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "forwardUrl",
                                "FORWARD_URL",
                                "LOADING"
                        ),
                        "forwardUrl"
                )
        );
        detailObjects.add(
                new PageObjContainer(
                        "addApiUrl",
                        PageObjContainer.PageObjType.link,
                        new LinkObjTemplate(
                                "addApiUrl",
                                "ADD_API",
                                MVCUtils.deleteUrlParams(
                                        routes.FrontEndFrameController.apiRegister(0).url()
                                ),
                                new AniMapBuilder<String, Object>()
                                        .put("appId", "id")
                                        .getMap()
                        ),
                        null
                )
        );
        detailObjects.add(
                new PageObjContainer(
                        "description",
                        PageObjContainer.PageObjType.article,
                        new TextFieldObjTemplate(
                                "description",
                                "APP_DESCRIPTION",
                                "LOADING"
                        ),
                        "description"
                )
        );
        detailObjects.add(
                apiInfoPageObj()
        );

        return new PageTemplate(
                "APP_DETAIL",
                "APP_DETAIL_PAGE",
                "APP_DETAIL",
                PageTemplate.PageType.detail.name(),
                MVCUtils.deleteUrlParams(routes.AniAppController.queryAppWithId(0L).url()),
                detailObjects
        );
    }
    private static PageObjContainer apiInfoPageObj(){
        return new PageObjContainer(
                "containedApi",
                PageObjContainer.PageObjType.list,
                new ListObjTemplate(
                        "API",
                        "APP_API_LIST",
                        null,
                        null,
                        null,
                        false,
                        null,
                        null,
                        apiInfoFields()
                ),
                "containedApi"
        );
    }
    private static List<PageObjContainer> apiInfoFields(){
        List<PageObjContainer> listFields = new ArrayList<PageObjContainer>();
        listFields.add(
                new PageObjContainer(
                        "apiId",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "apiId",
                                "API_ID",
                                "LOADING"
                        ),
                        "id"
                )
        );
        listFields.add(
                new PageObjContainer(
                        "apiName",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "apiName",
                                "API_NAME",
                                "LOADING"
                        ),
                        "apiName"
                )
        );
        listFields.add(
                new PageObjContainer(
                        "apiType",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "apiType",
                                "API_TYPE",
                                "LOADING"
                        ),
                        "type"
                )
        );
        listFields.add(
                new PageObjContainer(
                        "apiState",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "apiState",
                                "API_State",
                                "LOADING"
                        ),
                        "state"
                )
        );
        listFields.add(
                new PageObjContainer(
                        "httpMethod",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "httpMethod",
                                "HTTP_METHOD",
                                "LOADING"
                        ),
                        "method"
                )
        );
        listFields.add(
                new PageObjContainer(
                        "url",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "url",
                                "URL",
                                "LOADING"
                        ),
                        "url"
                )
        );
        listFields.add(
                new PageObjContainer(
                        "description",
                        PageObjContainer.PageObjType.article,
                        new TextFieldObjTemplate(
                                "description",
                                "DESCRIPTION",
                                "LOADING"
                        ),
                        "description"
                )
        );
        listFields.add(
                new PageObjContainer(
                        "params",
                        PageObjContainer.PageObjType.list,
                        new ListObjTemplate(
                                "PARAMS",
                                "API_PARAMS",
                                null,
                                null,
                                null,
                                false,
                                null,
                                null,
                                apiParamFields()
                        ),
                        "params"
                )
        );
        return listFields;
    }
    private static List<PageObjContainer> apiParamFields(){
        List<PageObjContainer> listFields = new ArrayList<PageObjContainer>();
        listFields.add(
                new PageObjContainer(
                        "paramName",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "paramName",
                                "PARAM_NAME",
                                "LOADING"
                        ),
                        "paramName"
                )
        );
        listFields.add(
                new PageObjContainer(
                        "paramDefaultValue",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "paramDafaultValue",
                                "PARAM_DEFAULT_VALUE",
                                "LOADING"
                        ),
                        "paramValue"
                )
        );
        listFields.add(
                new PageObjContainer(
                        "isRequired",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "isRequired",
                                "IS_REQUIRED",
                                "LOADING"
                        ),
                        "type"
                )
        );
        listFields.add(
                new PageObjContainer(
                        "description",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "description",
                                "DESCRIPTION",
                                "LOADING"
                        ),
                        "description"
                )
        );
        return listFields;
    }
    public static PageTemplate publicAppList(String targetDataUrl, String pageTitle, String pageDescription){
        List<ListObjTemplate> listObjTemplate = AniGeneralUtils.obtainSingleObjList(
                new ListObjTemplate(
                        pageTitle,
                        pageDescription,
                        "totalPageNum",
                        "page",
                        "keyword",
                        true,
                        "PAGE_{page}_OF_{totalPageNum}",
                        new AniMapBuilder<String, Object>()
                                .putAnyway("classificationId", 0)
                                .putAnyway("parentAppId", 0)
                                .getMap(),
                        generatePublicAppListPageObjectContainer()
                )
        );
        PageTemplate userInstalledAppListPage = new PageTemplate(
                pageTitle,
                pageDescription,
                pageTitle,
                PageTemplate.PageType.list.name(),
                targetDataUrl,
                listObjTemplate
        );
        return userInstalledAppListPage;
    }
    private static List<PageObjContainer> generatePublicAppListPageObjectContainer(){
        List<PageObjContainer> appListObjects = new ArrayList<PageObjContainer>();

        appListObjects.add(
                new PageObjContainer(
                    "appName",
                    PageObjContainer.PageObjType.forwardLink,
                    new LinkObjTemplate(
                            "appName",
                            "APP_NAME",
                            "forwardUrl",
                            null
                    ),
                    "appName"
            )
        );
        appListObjects.add(
                new PageObjContainer(
                        "detailButton",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "detailButton",
                                "APP_DETAIL",
                                "LOADING",
                                MVCUtils.deleteUrlParams(
                                        routes.FrontEndFrameController.appDetailsPage().url()
                                ),
                                new AniMapBuilder<String, Object>()
                                        .put("appId", "id")
                                        .getMap()
                        ),
                        null
                )
        );
        appListObjects.add(
                new PageObjContainer(
                        "description",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "description",
                                "APP_DESCRIPTION",
                                "LOADING"
                        ),
                        "description"
                )
        );
        appListObjects.add(
                new PageObjContainer(
                        "ownerUser",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "ownerUser",
                                "OWNER_USER",
                                "LOADING"
                        ),
                        "ownerUser"
                )
        );
        appListObjects.add(
                new PageObjContainer(
                        "appClassification",
                        PageObjContainer.PageObjType.field,
                        new TextFieldObjTemplate(
                                "appClassification",
                                "APP_CLASSIFICATION",
                                "LOADING"
                        ),
                        "appClassification.classificationKey"
                )
        );
        appListObjects.add(appInstallButton());
        appListObjects.add(appActivateButton());
        return appListObjects;
    }
    public static PageTemplate appRegForm(AniUser curUser){
        //set template
        PageTemplate appRegPage = new PageTemplate();
        appRegPage.pageType = PageTemplate.PageType.form.name();
        appRegPage.title = "REGISTER_APP";
        appRegPage.description = "APP_REGISTER_PAGE";
        appRegPage.containerId = "APP_REG_FORM";
        appRegPage.targetUrl = routes.AniAppController.registerApp().url();
        appRegPage.redirectUrl = routes.BaseController.index().url();//TODO
        appRegPage.targetButtonValue = "SUBMIT_APP";
        //form args
        List<FormObjTemplate> formObjs = new ArrayList<FormObjTemplate>();
        //appName
        formObjs.add(
                new FormObjTemplate(
                        "APP_NAME",
                        "appName",
                        FormObjTemplate.FormFieldMark.input.name(),
                        FormObjTemplate.FormInputProperties.text.name(),
                        null,
                        null,
                        null,
                        null,
                        null
                )
        );
        //description
        formObjs.add(
                new FormObjTemplate(
                        "APP_DESCRIPTION",
                        "description",
                        FormObjTemplate.FormFieldMark.textarea.name(),
                        null,
                        null,
                        null,
                        null,
                        null,
                        null
                )
        );
        //forwardUrl
        formObjs.add(
                new FormObjTemplate(
                        "FORWARD_URL",
                        "forwardUrl",
                        FormObjTemplate.FormFieldMark.input.name(),
                        FormObjTemplate.FormInputProperties.text.name(),
                        null,
                        null,
                        null,
                        null,
                        null
                )
        );
        //classificationId
        formObjs.add(
                new FormObjTemplate(
                        "APP_CLASSIFICATION",
                        "classificationId",
                        FormObjTemplate.FormFieldMark.select.name(),
                        null,
                        null,
                        null,
                        null,
                        AniAppService.getAppClassificationNames(),
                        null
                )
        );
        //typeId
        formObjs.add(
                new FormObjTemplate(
                        "APP_TYPE",
                        "typeId",
                        FormObjTemplate.FormFieldMark.select.name(),
                        null,
                        null,
                        null,
                        null,
                        AniPublicPropService.getObjectTypesName(),
                        null
                )
        );
        //relyOnAppId
        formObjs.add(
                new FormObjTemplate(
                        "RELY_ON_APP",
                        "relyOnAppId",
                        FormObjTemplate.FormFieldMark.select.name(),
                        null,
                        null,
                        null,
                        null,
                        AniAppService.getUserAppNames(curUser.email),
                        null
                )
        );
        //ownerUserEmail - ignore
        appRegPage.objects = formObjs;
        return appRegPage;
    }
    private static PageObjContainer appInstallButton(){
        String[] buttonLabels= new String[]{"INSTALL", "INSTALLING", "REMOVE", "REMOVING"};
        return new PageObjContainer(
                "installButton",
                PageObjContainer.PageObjType.button,
                new ButtonObjTemplate(
                        buttonLabels,
                        routes.AniAppController.installOrRemoveApps().url(),
                        new AniMapBuilder<String, Object>()
                                .put("appIds", "id")
                                .getMap()
                ),
                "isInstalled",
                "canInstall"
        );
    }
    private static PageObjContainer appActivateButton(){
        String[] buttonLabels= new String[]{"ACTIVATE", "ACTIVATING", "DEACTIVATE", "DEACTIVATING"};
        return new PageObjContainer(
                "activateButton",
                PageObjContainer.PageObjType.button,
                new ButtonObjTemplate(
                        buttonLabels,
                        routes.AniAppController.switchAppState().url(),
                        new AniMapBuilder<String, Object>()
                                .put("appId", "id")
                                .getMap()
                ),
                "isActivated",
                "canModify"
        );
    }
}
